import * as mobx from 'mobx';
import {action, asMap, observable, toJS} from 'mobx';
import base from './../util/base';
import * as type from './../constants/type';
import {doSubmitForm} from './../util/submit'
import * as API from './../apis/req'
import {getAntd, getComsMobx, getLabel} from '../util/convert';
import * as _ from '../util/public/common';

export class FreeWflowSimple {
    @observable showCollect = false;
    @observable form = null;
    toNewFormData = {};
    @action("控制变量")
    setState = (state = {}) => {
        Object.keys(state).forEach(key => {
            this[key] = state[key];
        });
    }

    onCloseDialog = () => {
        const { controlDialogVisible } = base.globalStore;
        controlDialogVisible(type.FREE_WORKFLOW_SIMPLE, false);
    }

    loadCondition = (params) => {
        API.loadCondition(params).then((data) => {
            const { WeaForm } = getComsMobx();
            this.conditionData  = data;
            const {conditioninfo,default_prefix = '',default_datas = {}} = data;
            let condition  =  this.setDefaultData(conditioninfo,default_prefix,default_datas);
            this.conditioninfo = condition;
            this.form = new WeaForm();
            this.form.setCondition(condition);
            this.form.initFormFields(condition);
            this.getFormSubmitDatas();

            let formParams = this.form.getFormParams();
            if (formParams.hasOwnProperty("isToNewAddSign") && formParams.isToNewAddSign == 1) {
                this.toNewFormData = this.form.getFormDatas();
            }
        });
    }

    /**
     * 设置默认值
     * @param conditioninfo
     * @param default_prefix
     * @param default_datas
     * @returns {Array}
     */
    setDefaultData = (conditioninfo = [],default_prefix = '',default_datas) => {
        //if(default_prefix == '') return conditioninfo;
        let { items = [] } = conditioninfo[0];
        let tempDefault_prefix = default_prefix;
        if(default_prefix == 'curr' && !_.has(default_datas,'curr_isvalild')){
            tempDefault_prefix = 'last';
        }
        let default_nodeOperator  = default_datas[`${tempDefault_prefix}_nodeOperator`] || [];
        let default_endTransfer  = default_datas[`${tempDefault_prefix}_endTransfer`] || 0;
        let default_countersignAttribute  = default_datas[`${tempDefault_prefix}_countersignAttribute`] || 0;
        let default_approvalSettings  = default_datas[`${tempDefault_prefix}_approvalSettings`] || '';
        const {isaffirmance = ''} = base.globalStore.urlParams;
        let viewAttr = 2;
        if(isaffirmance == 1) {
            viewAttr = 1;
        } else {
            if(default_prefix == 'curr' && _.has(default_datas,'last_isvalild')) viewAttr = 3;
            if(default_prefix == 'last') viewAttr = 1;
        }
        items.map(item => {
            if(isaffirmance == 1 || item.domkey[0] !== 'isToNewAddSign') {
                item.viewAttr = viewAttr;
            }
            if (item.domkey[0] == 'nodeOperator') {
                item.browserConditionParam.replaceDatas = default_nodeOperator;
            } else if (item.domkey[0] == 'endTransfer') {
                item.options.map(option => {
                    option.selected  = option.key == default_endTransfer;
                })
            } else if (item.domkey[0] == 'countersignAttribute') {
                item.options.map(option => {
                    option.selected  = option.key == default_countersignAttribute;
                })
            } else if (item.domkey[0] == 'approvalSettings') {
                item.value = default_approvalSettings;
            }
        });
        return conditioninfo;
    }

    changeForm = (v) => {
        if(v < 2) {
            const { WeaForm } = getComsMobx();
            let {conditioninfo,default_datas = {}} = mobx.toJS(this.conditionData);
            let condition  = this.setDefaultData(conditioninfo,v == 0 ? 'last' : 'curr',default_datas);
            this.conditioninfo = condition;
            this.form = new WeaForm();
            this.form.setCondition(condition);
            this.form.initFormFields(condition);
            if(v == 1) {
                this.form.updateFields(this.toNewFormData);
            }
        }
        this.form.updateFields({ "isToNewAddSign": v }, false);
        this.getFormSubmitDatas();
    }

    getFormSubmitDatas = () => {
        let formParams = this.form.getFormParams() || {}
        let submitParams = {
            operatorids: formParams.nodeOperator || "",
            countersignAttribute: formParams.countersignAttribute || "",
            isFlowToNext: formParams.endTransfer || "",
            isToNewAddSign: formParams.isToNewAddSign || "3",
            approvalSettings:formParams.approvalSettings ||''
        }
        this.chengeSubmitParams(submitParams);
    }

    chengeSubmitParams = (submitParams = {}) => {
        const { changeSubmitParam } = base.globalStore;
        changeSubmitParam(submitParams)
    }

    synchronousRemarkData = () => {
        const { controlVariableMap, variableMap } = base.globalStore;
        const remark_list = variableMap.get("remark_list") || [];
        const remark_list_freeWorkflowSimple = observable.map ? observable.map(remark_list) : asMap(remark_list);
        controlVariableMap("remark_list_freeWorkflowSimple", remark_list_freeWorkflowSimple);
    }

    updateFormFields = (updateParams = {}) => {
        this.form.updateFields(updateParams);
    }
}